<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Groups/classes/PhpFox_ComponentJoin.class.php, $Author:  $ */



/** Component for join group
 * @version $Id: PhpFox_ComponentJoin.class.php
 *  Max Kovrigovich
 * @package module.groups
 */
class PhpFox_Mod_Groups_ComponentJoin extends BaseComponent
{
    /** Processing method
     */

    function process()
    {
    	App::membersOnly();

        //check permissions
        $oSecurityService = App::getModuleService('Account', 'Security');
		/* @var $oSecurityService PhpFox_Mod_Account_ServiceSecurity */
		$oSecurityService->checkPerm('groups', array('read_only', 'full'));

        $oMessengerService = &App::getModuleService('Messenger', 'Messenger');

        $oGroupsService = $this->_oModule->getService('Groups');
        /* @var $oGroupsService PhpFox_Mod_Groups_ServiceGroups */

        $oTpl = $this->_oModule->getTemplate();

        $oReq = &App::getRequest();
        /* @var $oReq Request */

        $oUser = $this->getParam('user');

        $iGroupId = $oReq->getInt('id', 0);
        if(!$iGroupId)
            App::gotoUrl('public.groups');


	$aGroupInfo=$oGroupsService->getGroupData($iGroupId);
        if(!$aGroupInfo)
            App::gotoUrl('public.groups');

        $bIsMember = $oGroupsService->isMember($iGroupId, $oUser->aData['user']);

        $aGroupData = $oGroupsService->getGroupData($iGroupId);
        $bIsClosed = ($aGroupData['open_join'] == 'No');

        if(!$bIsMember && (!$bIsClosed OR App::isStaff()))
        {
            //add member to group
            $oGroupsService->addGroupMember($iGroupId, $oUser->aData['user'], $aGroupData['type']);
            $aD = $oGroupsService->getInviteData($iGroupId, $oUser->aData['user']);
            if(count($aD))
            {
                $oGroupsService->removeInvite($aD['id']);
            }

            //send notification
            if ( App::getSetting('new_friend') )
            {
            	$sText = App::format('Mod_Groups.new_member_text', array
            	(
					$oUser->aData['user'],
					App::makeUrl("public.groups.view",array('id' => $iGroupId)),
					$aGroupInfo['title']
            	));
            	$sText = preg_replace('/\<a href=(.*?)\>(.*?)\<\/a\>/', '[a=$1]$2[/a]', $sText);
            	$oMessengerService->sendMessage(array('to'=>$aGroupData['user'], 'title' => App::format('Mod_Groups.new_member_title'), 'text' => $sText), App::getSetting('new_friend'), 'mail');
            }
        }

        $oTpl->assign(array(
            'aGroupData'=> $aGroupData,
            'isMember' => $bIsMember,
            'isClosed' => $bIsClosed,
            'sImageUrl'=> App::getParam('groups.pic.url'),
        ));
        $sHtml = $oTpl->fetch('Join.html');
        return $sHtml;
    }
}
?>